<template>
  <div class="push-notification-container">
    <!-- 推送通知弹窗 -->
    <div 
      v-if="showNotification" 
      class="push-notification"
      :class="{ 'slide-in': showNotification }"
      @click="handleNotificationClick"
    >
      <div class="notification-content">
        <div class="notification-icon">
          <el-icon><Bell /></el-icon>
        </div>
        <div class="notification-text">
          <div class="notification-title">{{ currentNotification.title }}</div>
          <div class="notification-body">{{ currentNotification.content }}</div>
        </div>
        <div class="notification-close" @click.stop="closeNotification">
          <el-icon><Close /></el-icon>
        </div>
      </div>
    </div>

    <!-- 推送历史列表 -->
    <el-drawer
      v-model="showHistory"
      title="推送通知"
      direction="rtl"
      size="400px"
    >
      <div class="push-history">
        <div class="history-header">
          <el-button type="primary" @click="refreshHistory">
            <el-icon><Refresh /></el-icon>
            刷新
          </el-button>
          <el-button @click="clearHistory">
            <el-icon><Delete /></el-icon>
            清空
          </el-button>
        </div>
        
        <div class="history-list" v-loading="loadingHistory">
          <div 
            v-for="item in pushHistory" 
            :key="item.id"
            class="history-item"
            :class="{ 'unread': !item.readTime }"
            @click="handleHistoryItemClick(item)"
          >
            <div class="item-icon">
              <el-icon><Document /></el-icon>
            </div>
            <div class="item-content">
              <div class="item-title">{{ item.title || '新任务推荐' }}</div>
              <div class="item-time">{{ formatTime(item.pushTime) }}</div>
            </div>
            <div class="item-status">
              <el-tag 
                :type="getStatusTagType(item.pushStatus)" 
                size="small"
              >
                {{ getStatusText(item.pushStatus) }}
              </el-tag>
            </div>
          </div>
          
          <div v-if="pushHistory.length === 0" class="empty-history">
            <el-empty description="暂无推送记录" />
          </div>
        </div>
      </div>
    </el-drawer>

    <!-- 推送设置 -->
    <el-drawer
      v-model="showSettings"
      title="推送设置"
      direction="rtl"
      size="400px"
    >
      <div class="push-settings">
        <el-form :model="pushSettings" label-width="120px">
          <el-form-item label="推送开关">
            <el-switch v-model="pushSettings.enabled" @change="updatePushSettings" />
          </el-form-item>
          
          <el-form-item label="任务推送">
            <el-switch v-model="pushSettings.taskPush" @change="updatePushSettings" />
          </el-form-item>
          
          <el-form-item label="系统通知">
            <el-switch v-model="pushSettings.systemNotification" @change="updatePushSettings" />
          </el-form-item>
          
          <el-form-item label="推送声音">
            <el-switch v-model="pushSettings.sound" @change="updatePushSettings" />
          </el-form-item>
          
          <el-form-item label="推送震动">
            <el-switch v-model="pushSettings.vibration" @change="updatePushSettings" />
          </el-form-item>
          
          <el-form-item label="免打扰时间">
            <el-time-picker
              v-model="pushSettings.quietStart"
              placeholder="开始时间"
              format="HH:mm"
              value-format="HH:mm"
              @change="updatePushSettings"
            />
            <span style="margin: 0 10px;">至</span>
            <el-time-picker
              v-model="pushSettings.quietEnd"
              placeholder="结束时间"
              format="HH:mm"
              value-format="HH:mm"
              @change="updatePushSettings"
            />
          </el-form-item>
        </el-form>
        
        <div class="device-info">
          <h4>设备信息</h4>
          <el-descriptions :column="1" border>
            <el-descriptions-item label="设备类型">{{ deviceInfo.type }}</el-descriptions-item>
            <el-descriptions-item label="设备品牌">{{ deviceInfo.brand }}</el-descriptions-item>
            <el-descriptions-item label="设备型号">{{ deviceInfo.model }}</el-descriptions-item>
            <el-descriptions-item label="应用版本">{{ deviceInfo.appVersion }}</el-descriptions-item>
            <el-descriptions-item label="系统版本">{{ deviceInfo.osVersion }}</el-descriptions-item>
          </el-descriptions>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Bell, Close, Refresh, Delete, Document } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/user'
import { NETWORK_CONFIG } from '@/config/network.js'
import dayjs from 'dayjs'

const userStore = useUserStore()

// 推送通知状态
const showNotification = ref(false)
const currentNotification = ref({})
const notificationTimer = ref(null)

// 推送历史
const showHistory = ref(false)
const loadingHistory = ref(false)
const pushHistory = ref([])

// 推送设置
const showSettings = ref(false)
const pushSettings = reactive({
  enabled: true,
  taskPush: true,
  systemNotification: true,
  sound: true,
  vibration: true,
  quietStart: '22:00',
  quietEnd: '08:00'
})

// 设备信息
const deviceInfo = reactive({
  type: '',
  brand: '',
  model: '',
  appVersion: '',
  osVersion: ''
})

// WebSocket连接
let websocket = null
const reconnectTimer = ref(null)
const reconnectAttempts = ref(0)
const maxReconnectAttempts = 5

// 初始化推送系统
const initPushSystem = async () => {
  try {
    // 获取设备信息
    await getDeviceInfo()
    
    // 注册设备
    await registerDevice()
    
    // 建立WebSocket连接
    connectWebSocket()
    
    // 加载推送历史
    await loadPushHistory()
    
    // 请求推送权限
    requestNotificationPermission()
    
  } catch (error) {
    console.error('初始化推送系统失败:', error)
  }
}

// 获取设备信息
const getDeviceInfo = () => {
  // 检测设备类型
  const ua = navigator.userAgent.toLowerCase()
  
  if (ua.includes('iphone') || ua.includes('ipad')) {
    deviceInfo.type = 'ios'
    deviceInfo.brand = 'apple'
  } else if (ua.includes('android')) {
    deviceInfo.type = 'android'
    
    if (ua.includes('xiaomi')) {
      deviceInfo.brand = 'xiaomi'
    } else if (ua.includes('huawei')) {
      deviceInfo.brand = 'huawei'
    } else if (ua.includes('oppo')) {
      deviceInfo.brand = 'oppo'
    } else if (ua.includes('vivo')) {
      deviceInfo.brand = 'vivo'
    } else if (ua.includes('oneplus')) {
      deviceInfo.brand = 'oneplus'
    } else {
      deviceInfo.brand = 'other'
    }
  } else {
    deviceInfo.type = 'web'
    deviceInfo.brand = 'web'
  }
  
  // 获取应用版本和系统版本
  deviceInfo.appVersion = '1.0.0' // 从应用配置获取
  deviceInfo.osVersion = navigator.platform
}

// 注册设备
const registerDevice = async () => {
  try {
    const userId = userStore.userInfo?.uid
    if (!userId) return
    
    const deviceData = {
      userId: userId,
      deviceToken: await getDeviceToken(),
      deviceType: deviceInfo.type,
      deviceBrand: deviceInfo.brand,
      deviceModel: deviceInfo.model,
      appVersion: deviceInfo.appVersion,
      osVersion: deviceInfo.osVersion
    }
    
    const response = await uni.request({
      url: `${NETWORK_CONFIG.API_BASE_URL}/api/push/device/register`,
      method: 'POST',
      data: deviceData,
      header: { 'Access-Token': `Bearer ${userStore.token}` }
    })
    
    if (response.data.success) {
      console.log('设备注册成功')
    } else {
      console.error('设备注册失败:', response.data.message)
    }
    
  } catch (error) {
    console.error('设备注册失败:', error)
  }
}

// 获取设备Token
const getDeviceToken = async () => {
  // 这里应该调用各平台的推送SDK获取设备Token
  // 简化版本，返回一个模拟Token
  return `device_token_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
}

// 建立WebSocket连接
const connectWebSocket = () => {
  try {
    const userId = userStore.userInfo?.uid
    if (!userId) return
    
    const wsUrl = `ws://192.168.2.2:8080/ws/task-push?userId=${userId}`
    websocket = new WebSocket(wsUrl)
    
    websocket.onopen = () => {
      console.log('WebSocket连接已建立')
      reconnectAttempts.value = 0
      
      // 发送心跳
      startHeartbeat()
    }
    
    websocket.onmessage = (event) => {
      try {
        const message = JSON.parse(event.data)
        handleWebSocketMessage(message)
      } catch (error) {
        console.error('解析WebSocket消息失败:', error)
      }
    }
    
    websocket.onclose = () => {
      console.log('WebSocket连接已关闭')
      stopHeartbeat()
      
      // 尝试重连
      if (reconnectAttempts.value < maxReconnectAttempts) {
        reconnectTimer.value = setTimeout(() => {
          reconnectAttempts.value++
          console.log(`尝试重连WebSocket (${reconnectAttempts.value}/${maxReconnectAttempts})`)
          connectWebSocket()
        }, 3000 * reconnectAttempts.value)
      }
    }
    
    websocket.onerror = (error) => {
      console.error('WebSocket连接错误:', error)
    }
    
  } catch (error) {
    console.error('建立WebSocket连接失败:', error)
  }
}

// 处理WebSocket消息
const handleWebSocketMessage = (message) => {
  switch (message.type) {
    case 'task_push':
      showTaskPushNotification(message)
      break
      
    case 'system_notification':
      showSystemNotification(message)
      break
      
    case 'pong':
      // 心跳响应
      break
      
    case 'error':
      ElMessage.error(message.title || '推送消息处理失败')
      break
      
    default:
      console.log('未知WebSocket消息类型:', message.type)
  }
}

// 显示任务推送通知
const showTaskPushNotification = (message) => {
  if (!pushSettings.enabled || !pushSettings.taskPush) return
  
  // 检查免打扰时间
  if (isQuietTime()) return
  
  currentNotification.value = {
    id: message.data.pushId,
    taskId: message.data.taskId,
    title: message.title,
    content: message.data.content,
    timestamp: message.data.timestamp
  }
  
  showNotification.value = true
  
  // 播放声音
  if (pushSettings.sound) {
    playNotificationSound()
  }
  
  // 震动
  if (pushSettings.vibration) {
    vibrate()
  }
  
  // 显示浏览器通知
  showBrowserNotification(message.title, message.data.content)
  
  // 自动关闭
  notificationTimer.value = setTimeout(() => {
    closeNotification()
  }, 5000)
}

// 显示系统通知
const showSystemNotification = (message) => {
  if (!pushSettings.enabled || !pushSettings.systemNotification) return
  
  ElMessage.info(message.title)
  
  // 显示浏览器通知
  showBrowserNotification(message.title, message.data.content)
}

// 显示浏览器通知
const showBrowserNotification = (title, body) => {
  if (!('Notification' in window)) return
  
  if (Notification.permission === 'granted') {
    const notification = new Notification(title, {
      body: body,
      icon: '/icon.png',
      badge: '/badge.png',
      tag: 'task-push'
    })
    
    notification.onclick = () => {
      window.focus()
      notification.close()
    }
    
    // 5秒后自动关闭
    setTimeout(() => {
      notification.close()
    }, 5000)
  }
}

// 请求推送权限
const requestNotificationPermission = async () => {
  if (!('Notification' in window)) return
  
  if (Notification.permission === 'default') {
    const permission = await Notification.requestPermission()
    console.log('推送权限状态:', permission)
  }
}

// 检查是否在免打扰时间
const isQuietTime = () => {
  if (!pushSettings.quietStart || !pushSettings.quietEnd) return false
  
  const now = dayjs()
  const start = dayjs(pushSettings.quietStart, 'HH:mm')
  const end = dayjs(pushSettings.quietEnd, 'HH:mm')
  
  if (start.isBefore(end)) {
    return now.isAfter(start) && now.isBefore(end)
  } else {
    // 跨天的情况
    return now.isAfter(start) || now.isBefore(end)
  }
}

// 播放通知声音
const playNotificationSound = () => {
  try {
    const audio = new Audio('/notification.mp3')
    audio.play().catch(() => {
      // 静默处理播放失败
    })
  } catch (error) {
    // 静默处理错误
  }
}

// 震动
const vibrate = () => {
  if ('vibrate' in navigator) {
    navigator.vibrate([200, 100, 200])
  }
}

// 心跳检测
const startHeartbeat = () => {
  const heartbeat = () => {
    if (websocket && websocket.readyState === WebSocket.OPEN) {
      websocket.send(JSON.stringify({ type: 'ping' }))
    }
  }
  
  // 每30秒发送一次心跳
  setInterval(heartbeat, 30000)
}

const stopHeartbeat = () => {
  // 清理心跳定时器
}

// 关闭通知
const closeNotification = () => {
  showNotification.value = false
  if (notificationTimer.value) {
    clearTimeout(notificationTimer.value)
    notificationTimer.value = null
  }
}

// 处理通知点击
const handleNotificationClick = () => {
  if (currentNotification.value.taskId) {
    // 跳转到任务详情页
    uni.navigateTo({
      url: `/pages/task/detail?id=${currentNotification.value.taskId}`
    })
    
    // 记录点击
    recordPushClick(currentNotification.value.id)
  }
  
  closeNotification()
}

// 记录推送点击
const recordPushClick = async (pushId) => {
  try {
    await uni.request({
      url: `${NETWORK_CONFIG.API_BASE_URL}/api/push/click/${pushId}`,
      method: 'POST',
      header: { 'Access-Token': `Bearer ${userStore.token}` }
    })
  } catch (error) {
    console.error('记录推送点击失败:', error)
  }
}

// 记录推送阅读
const recordPushRead = async (pushId) => {
  try {
    await uni.request({
      url: `${NETWORK_CONFIG.API_BASE_URL}/api/push/read/${pushId}`,
      method: 'POST',
      header: { 'Access-Token': `Bearer ${userStore.token}` }
    })
  } catch (error) {
    console.error('记录推送阅读失败:', error)
  }
}

// 加载推送历史
const loadPushHistory = async () => {
  try {
    loadingHistory.value = true
    const userId = userStore.userInfo?.uid
    if (!userId) return
    
    const response = await uni.request({
      url: `${NETWORK_CONFIG.API_BASE_URL}/api/push/history/${userId}`,
      method: 'GET',
      data: { offset: 0, limit: 50 },
      header: { 'Access-Token': `Bearer ${userStore.token}` }
    })
    
    if (response.data.success) {
      pushHistory.value = response.data.data
    }
    
  } catch (error) {
    console.error('加载推送历史失败:', error)
  } finally {
    loadingHistory.value = false
  }
}

// 刷新历史
const refreshHistory = () => {
  loadPushHistory()
}

// 清空历史
const clearHistory = () => {
  pushHistory.value = []
}

// 处理历史项点击
const handleHistoryItemClick = (item) => {
  if (item.taskId) {
    uni.navigateTo({
      url: `/pages/task/detail?id=${item.taskId}`
    })
    
    // 记录阅读
    if (!item.readTime) {
      recordPushRead(item.id)
    }
  }
}

// 更新推送设置
const updatePushSettings = () => {
  // 保存设置到本地存储
  uni.setStorageSync('pushSettings', pushSettings)
}

// 获取状态标签类型
const getStatusTagType = (status) => {
  const statusMap = {
    'pending': 'warning',
    'sent': 'primary',
    'delivered': 'success',
    'failed': 'danger'
  }
  return statusMap[status] || 'info'
}

// 获取状态文本
const getStatusText = (status) => {
  const statusMap = {
    'pending': '待发送',
    'sent': '已发送',
    'delivered': '已送达',
    'failed': '发送失败'
  }
  return statusMap[status] || status
}

// 格式化时间
const formatTime = (time) => {
  return dayjs(time).format('MM-DD HH:mm')
}

// 暴露方法给父组件
defineExpose({
  showHistory: () => { showHistory.value = true },
  showSettings: () => { showSettings.value = true },
  initPushSystem
})

onMounted(() => {
  // 加载保存的设置
  const savedSettings = uni.getStorageSync('pushSettings')
  if (savedSettings) {
    Object.assign(pushSettings, savedSettings)
  }
  
  // 初始化推送系统
  initPushSystem()
})

onUnmounted(() => {
  // 清理资源
  if (websocket) {
    websocket.close()
  }
  
  if (reconnectTimer.value) {
    clearTimeout(reconnectTimer.value)
  }
  
  if (notificationTimer.value) {
    clearTimeout(notificationTimer.value)
  }
})
</script>

<style scoped>
.push-notification-container {
  position: relative;
}

.push-notification {
  position: fixed;
  top: 20px;
  right: 20px;
  width: 320px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  z-index: 9999;
  transform: translateX(100%);
  transition: transform 0.3s ease;
}

.push-notification.slide-in {
  transform: translateX(0);
}

.notification-content {
  display: flex;
  align-items: center;
  padding: 16px;
}

.notification-icon {
  margin-right: 12px;
  color: #409eff;
  font-size: 20px;
}

.notification-text {
  flex: 1;
}

.notification-title {
  font-weight: 500;
  color: #303133;
  margin-bottom: 4px;
}

.notification-body {
  font-size: 14px;
  color: #606266;
  line-height: 1.4;
}

.notification-close {
  margin-left: 12px;
  color: #909399;
  cursor: pointer;
  font-size: 16px;
}

.push-history {
  padding: 20px 0;
}

.history-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.history-list {
  max-height: 500px;
  overflow-y: auto;
}

.history-item {
  display: flex;
  align-items: center;
  padding: 12px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  transition: background-color 0.2s;
}

.history-item:hover {
  background-color: #f5f7fa;
}

.history-item.unread {
  background-color: #f0f9ff;
}

.item-icon {
  margin-right: 12px;
  color: #409eff;
  font-size: 18px;
}

.item-content {
  flex: 1;
}

.item-title {
  font-weight: 500;
  color: #303133;
  margin-bottom: 4px;
}

.item-time {
  font-size: 12px;
  color: #909399;
}

.item-status {
  margin-left: 12px;
}

.empty-history {
  text-align: center;
  padding: 40px 0;
}

.push-settings {
  padding: 20px 0;
}

.device-info {
  margin-top: 30px;
}

.device-info h4 {
  color: #606266;
  margin-bottom: 15px;
}

:deep(.el-descriptions__label) {
  font-weight: 500;
}
</style>
